package com.gxuwz.stats.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.stats.entity.ResearchProject;
import com.gxuwz.stats.entity.User;
import com.gxuwz.stats.service.ResearchProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超级管理员-科研项目工作量
 */
@RestController
@RequestMapping("/researchProject")
public class ResearchProjectController {

    @Autowired
    private ResearchProjectService researchProjectService;

    /**
     * 分页查询
     * @param params
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/researchProjectList")
    public ResponseEntity<Map<String, Object>> researchProjectList(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<ResearchProject> page = new Page<>(currentPage, pageSize);
            QueryWrapper<ResearchProject> queryWrapper = new QueryWrapper<>();

            // 模糊查询条件
            if (params.containsKey("userKeyword") && !params.get("userKeyword").isEmpty()) {
                String keyword = "%" + params.get("userKeyword") + "%";
                queryWrapper.and(wrapper -> wrapper
                        .like("username", keyword)
                        .or().like("role", keyword)
                        .or().like("degree", keyword)
                        .or().like("position", keyword)
                        .or().like("post", keyword)
                        .or().like("department", keyword));
            }

            // 调用 service 层的分页查询方法
            IPage<ResearchProject> resultPage = researchProjectService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    @PostMapping("/importData")
    public ResponseEntity<Map<String, Object>> importData(@RequestParam("file") MultipartFile file) {
        try {
            researchProjectService.importResearchProjectData(file);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "数据导入成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "数据导入失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    /**
     * 新增
     */
    @PostMapping("/addResearchProject")
    public ResponseEntity<Map<String, Object>> addResearchProject(@RequestBody ResearchProject researchProject) {
        System.out.println("===" + researchProject);

        researchProjectService.save(researchProject);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "新增成功");
        return ResponseEntity.ok(response);
    }

    /**
     * 删除
     * @param projectIds
     * @return
     */
    @DeleteMapping("/deleteResearchProject")
    public ResponseEntity<Map<String, Object>> deleteResearchProject(@RequestBody List<Long> projectIds) {
        System.out.println("删除：" + projectIds);
        researchProjectService.removeByIds(projectIds);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "删除成功");
        return ResponseEntity.ok(response);
    }

    /**
     * 修改
     * @param researchProject
     * @return
     */
    @PutMapping("/editResearchProject")
    public ResponseEntity<Map<String, Object>> editResearchProject(@RequestBody ResearchProject researchProject) {
        System.out.println("user::" + researchProject);
        researchProjectService.updateById(researchProject);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "修改成功");
        return ResponseEntity.ok(response);
    }
}
